<?php

//好站模型
class Site_model extends Model {

	function Site_model()
	{
		parent::Model();	
		$this->load->database();
		$this->load->model("sitestatis_model", "ssm");
		$this->load->model("message_model");
		$this->load->model("options_model", "om");
		$this->load->library('smtp');
	}
	
	//认证选择账户
	function certselected($delstr, $updata = 1)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		//屏蔽会员
		$data = array(
			"certification" => $updata,
		);
		$this->db->where_in("id", $delid);
		$result = $this->db->update("site", $data);
		return $result;
	}
	
	//推荐小站
	function bestrecommendselected($delstr, $updata = 1)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		//屏蔽会员
		$data = array(
			"bestrecommend" => $updata,
		);
		$this->db->where_in("id", $delid);
		$result = $this->db->update("site", $data);
		return $result;
	}
	
	//推荐小站
	function recommendselected($delstr, $updata = 1)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		//屏蔽会员
		$data = array(
			"recommend" => $updata,
		);
		$this->db->where_in("id", $delid);
		$result = $this->db->update("site", $data);
		return $result;
	}
	
	//看看小站
	function kankanselected($delstr, $updata = 1)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		//屏蔽会员
		$data = array(
			"kankan" => $updata,
		);
		$this->db->where_in("id", $delid);
		$result = $this->db->update("site", $data);
		return $result;
	}
	
	//推荐小站
	function recommendselectedstatic($delstr, $field, $updata = 1)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		if ($field == 'all') {
			$data = array(
				"staticboy" => 0,
				"staticgirl" => 0,
			);
		} else {
			$field == 'girl' ? $field = "staticgirl" : $field = "staticboy";
			$data = array(
				$field => $updata,
			);
		}
		$this->db->where_in("id", $delid);
		$result = $this->db->update("site", $data);
		return $result;
	}
	
	//审核选择账户
	function statusselected($delstr, $updata = 1)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		//屏蔽会员
		$data = array(
			"status" => $updata,
		);
		$this->db->where_in("id", $delid);
		$result = $this->db->update("site", $data);
		
		$data = array(
			"status" => $updata,
		);
		$this->db->where_in("siteid", $delid);
		$result = $this->db->update("site_statis", $data);
		
		if($updata == '1') {
			for ($i=0; $i<count($delid); $i++) {
				//发送站内信息
				$siteid = $delid[$i];
				$site = $this->get_site($siteid);
				$userid = $site['userid'];
				$title = "系统通知";
				$description = "尊贵的用户，恭喜您！您申请的轻站: ".$site['name'].", 已经通过审核。";
				$this->message_model->sys_send($userid, $title, $description);
				
				//发送邮件
				$link = c('site_url').$site['domain'];
				$account = $this->am->get($userid);
				$email = $account['email'];
				$mailsubject = "热闹网系统邮件";//邮件主题
				$mailbody = "尊贵的用户，恭喜您！您申请的轻站: ".$site['name'].", 已经通过审核。请点击<a href='".$link."' target='_blank'>进入小站</a>。<br>了解更多关于热闹网信息，请进入热闹网：<a href='http://www.renao100.com' target='_blank'>http://www.renao100.com</a>";//邮件内容
				$mailtype = "HTML";//邮件格式（HTML/TXT）,TXT为文本邮件
				$this->smtp->debug = false;//是否显示发送的调试信息
				$this->smtp->sendmail($email, $mailsubject, $mailbody, $mailtype);
			}
		}		
		return $result;
	}
	
	//小站所有列表
	function sitelists()
	{
		$query = $this->db->from("site")->where("complete", 1)->order_by("id", "desc")->get();
		return $query->result_array();
	}
	
	//获取列表
	function listitem2($offset = 0, $pagecount = 20, $order = "id", $desc = "desc", $keyword = "", $topclassid = 0, $subclassid = 0)
	{
		$items = array();
		if($order == '0') $order = "id";
		if(!in_array($desc, array("desc", "asc"))) $desc = "desc";
//		$query = $this->db->from("site")->limit($pagecount, $offset)->order_by($order, $desc)->get();
		
		if(in_array($order, array('id', 'regtime', 'status'))) {
			$order = "s.".$order;
		} elseif (in_array($order, array('fans', 'visits'))) {
			$order = "ss.".$order;
		} else {
			$order = "s.id";
		}
		$sql = "select s.*,i.siteid,i.logo,ss.siteid, ss.fans,ss.visits, replace(s.name,'$keyword', '<em class=word_s>$keyword</em>') as title from rn_site as s left join rn_site_info as i on i.siteid=s.id left join rn_site_statis ss on ss.siteid=s.id left join rn_classes as c on c.id=s.classid where s.complete=1 and (LOCATE('$keyword', s.name)>0) ";
		//分类条件
		if($subclassid>0) {
			$sql .= " and s.classid = $subclassid";
		} elseif ($topclassid>0) {
			$sql .= " and c.parentid = $topclassid";
		}
		$sql .= " order by $order $desc";
		if ($pagecount>0) {
			$sql .= " limit $offset,$pagecount";
		}
		$query = $this->db->query($sql);
//		echo $this->db->last_query();
		if ($query->num_rows()>0) {
			$items = $query->result_array();
			foreach ($items as $key => $item) {
				$userid = $item['userid'];
				$row = $this->am->get($userid);
				$items[$key]['username'] = $row["username"];
			}
		}
		return $items;
	}
	
	//获取列表
	function listitem($offset = 0, $pagecount = 20, $order = "id", $desc = "desc", $keyword = "")
	{
		$items = array();
		if($order == '0') $order = "id";
		if(!in_array($desc, array("desc", "asc"))) $desc = "desc";
//		$query = $this->db->from("site")->limit($pagecount, $offset)->order_by($order, $desc)->get();
		
		if($keyword == "") {//所有
			if(in_array($order, array('id', 'regtime', 'status'))) {
				$order = "site.".$order;
			} elseif (in_array($order, array('fans', 'visits'))) {
				$order = "site_statis.".$order;
			} else {
				$order = "site.id";
			}
			$this->db->select("site.*,site_info.logo, site_statis.siteid, site_statis.fans,site_statis.visits");
			$this->db->from("site");
			$this->db->join("site_statis", "site_statis.siteid = site.id");
			$this->db->join("site_info", "site_info.siteid = site.id");
			if($pagecount > 0) {
				$this->db->limit($pagecount, $offset);
			}
			$this->db->order_by($order, $desc);
			$query = $this->db->get();
		} else {//搜索
			if(in_array($order, array('id', 'regtime', 'status'))) {
				$order = "s.".$order;
			} elseif (in_array($order, array('fans', 'visits'))) {
				$order = "ss.".$order;
			} else {
				$order = "s.id";
			}
			$sql = "select s.*,i.siteid,i.logo,ss.siteid, ss.fans,ss.visits, replace(s.name,'$keyword', '<em class=word_s>$keyword</em>') as title from rn_site as s left join rn_site_info as i on i.siteid=s.id left join rn_site_statis ss on ss.siteid=s.id where s.complete=1 and (LOCATE('$keyword', s.name)>0) order by $order $desc ";
			if ($pagecount>0) {
				$sql .= " limit $offset,$pagecount";
			}
			$query = $this->db->query($sql);	
		}
		
//		echo $this->db->last_query();
		if ($query->num_rows()>0) {
			$items = $query->result_array();
			foreach ($items as $key => $item) {
				$userid = $item['userid'];
				$row = $this->am->get($userid);
				$items[$key]['username'] = $row["username"];
			}
		}
		return $items;
	}
	
	//获取总数
	function countitems($keyword = "")
	{
		if ($keyword == "") {//所有
			$query = $this->db->get_where("site", array("complete"=>1));
			return $query->num_rows();
		} else {//搜索
			$query = $this->db->query("select s.*,i.siteid,i.logo, replace(s.name,'$keyword', '<em class=word_s>$keyword</em>') as title from rn_site as s left join rn_site_info as i on i.siteid=s.id where s.complete=1 and (LOCATE('$keyword', s.name)>0)");
			return $query->num_rows();	
		}		
	}
	
	//申请好站STEP1
	function opening_step1($sitedata = NULL)
	{
		if(is_null($sitedata)) return false;
		//验证
		if($this->exsits_name($sitedata["name"])) return false;
		if($this->exsits_domain($sitedata["domain"])) return false;
		//execute sql
		
		//info 表数据
		$infodata = array(
			"description" => $sitedata["description"],
		);
		//base 表数据
		unset($sitedata["description"]);
		$sitedata["regtime"] = time();
		//Execute SQL
		$sitedata["status"] = 1;//1)默认通过审核
		$this->db->insert("site", $sitedata);
		$siteid = $this->db->insert_id();
		//siteid
		$infodata["siteid"] = $siteid;
		$result = $this->db->insert("site_info", $infodata);
		return $siteid;
	}
	
	//申请好站STEP2
	function opening_step2($sitedata = NULL, $userid = 0, $username = "")
	{
		if(is_null($sitedata)) return false;
		//验证
		$siteid = $sitedata["siteid"];
		if(!$row = $this->get_site($siteid)) return false;
		unset($sitedata["siteid"]);
		//a($sitedata);
		//Execute SQL
		$this->db->where("siteid", $siteid);
		$result = $this->db->update("site_info", $sitedata);
		//创建默认选项
		$data = array(
			"siteid" => $siteid,
			"opname" => "选项一",
			"addtime" => time(),
		);
		$this->db->insert("site_options", $data);
		//创建统计纪录
		$data = array(
		"siteid" => $siteid,
		"classid" => $row["classid"],
		"fans" => 1,//默认自己为粉丝
		"status" => 1,//2)默认通过审核
		);
		$result = $this->db->insert("site_statis", $data);
		//将自己作为粉丝记录
		$data = array(
		"siteid" => $siteid,
		"userid" => $userid,
		"username" => $username,
		"jointime" => time(),
		"self" => 1,//自己
		);
		$result = $this->db->insert("fans", $data);
		//更新完成状态
		$this->update_complete($siteid);
		
		//3)默认通过审核
		//===========================================================================
		//发送站内信息
		$site = $this->get_site($siteid);
		$userid = $site['userid'];
		$title = "系统通知";
		$description = "尊贵的用户，恭喜您！您申请的轻站: ".$site['name'].", 已经通过审核。";
		$this->message_model->sys_send($userid, $title, $description);

		//发送邮件
		$link = c('site_url').$site['domain'];
		$account = $this->am->get($userid);
		$email = $account['email'];
		$mailsubject = "热闹网系统邮件";//邮件主题
		$mailbody = "尊贵的用户，恭喜您！您申请的轻站: ".$site['name'].", 已经通过审核。请点击<a href='".$link."' target='_blank'>进入小站</a>。<br>了解更多关于热闹网信息，请进入热闹网：<a href='http://www.renao100.com' target='_blank'>http://www.renao100.com</a>";//邮件内容
		$mailtype = "HTML";//邮件格式（HTML/TXT）,TXT为文本邮件
		$this->smtp->debug = false;//是否显示发送的调试信息
		$this->smtp->sendmail($email, $mailsubject, $mailbody, $mailtype);
		
		return $result;
	}
	
	//检测开好站AUTH
	function check_openauth($auth = NULL)
	{
		if(is_null($auth)) return false;
//		$auth = explode("_", $auth);
//		if(!isset($auth[0]) || !isset($auth[1])) return false;
//		$name = $auth[0];
//		$siteid = $auth[1];
//		$query = $this->db->get_where("site", array("id"=>$siteid, "name"=>$name, "complete"=>0));
		$siteid = $auth;
		$query = $this->db->get_where("site", array("id"=>$siteid, "complete"=>0));
		if($query->num_rows()>0) {
			$this->userid = $this->am->is_logined();
			$row = $query->row_array();
			if($row["userid"] != $this->userid) {
				return false;
			}
			return $siteid;
		}
		return false;
	}
	
	//检测好站是否存在
	function check_domainsite($domain)
	{
		$query = $this->db->get_where("site", array("domain"=>$domain, "complete"=>1, "status"=>1));
		if($query->num_rows()>0) {
			return true;
		}
		return false;
	}
	
	//删除选择小站
	function delselected($delstr)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		//删除
		$this->db->where_in("id", $delid);
		//site
		$this->db->delete("site");

		//site_info
		$this->db->where_in("siteid", $delid);
		$this->db->delete("site_info");
		//options
		$this->db->where_in("siteid", $delid);
		$this->db->delete("site_options");
		//site_statis 
		$this->db->where_in("siteid", $delid);
		$this->db->delete("site_statis");
		//fans 
		$this->db->where_in("siteid", $delid);
		$this->db->delete("fans");
		//module_forum
		$this->db->where_in("siteid", $delid);
		$this->db->delete("module_forum");
		//module_forum_comment
		$this->db->where_in("siteid", $delid);
		$this->db->delete("module_forum_comment");
		//fresh
		$this->db->where_in("siteid", $delid);
		$this->db->delete("fresh");
		return true;
	}
	
	//删除小站
	function delete($siteid)
	{
		//site
		$this->db->delete("site", array("id"=>$siteid));
		//site_info
		$this->db->delete("site_info", array("siteid"=>$siteid));
		
		//删除选项内容:热点、新闻、公告、专题、投票、视频、活动
		$this->om->del_siteoptions($siteid);
		
		//options
		$this->db->delete("site_options", array("siteid"=>$siteid));
		//site_statis 
		$this->db->delete("site_statis", array("siteid"=>$siteid));
		//fans 
		$this->db->delete("fans", array("siteid"=>$siteid));
		//module_forum
		$this->db->delete("module_forum", array("siteid"=>$siteid));
		//module_forum_comment
		$this->db->delete("module_forum_comment", array("siteid"=>$siteid));
		//fresh
		$this->db->delete("fresh", array("siteid"=>$siteid));
	}
	
	//更新完成状态
	function update_complete($siteid, $val = 1)
	{
		$data = array("complete" => $val);
		$this->db->where("id", $siteid);
		$result = $this->db->update("site", $data);
		return $result;
	}
	
	//更新基本信息
	function update_site($siteid, $data)
	{
		$this->db->where("id", $siteid);
		$result = $this->db->update("site", $data);
		return $result;
	}
	
	//更新信息
	function update_siteinfo($siteid, $data)
	{
		$this->db->where("siteid", $siteid);
		$result = $this->db->update("site_info", $data);
		return $result;
	}
	
	//获取好站全部信息
	function get($siteid)
	{
		$this->db->select("site.*, site_info.siteid, site_info.realname, site_info.usermail,site_info.phone, site_info.logo, site_info.color, site_info.background, site_info.skin, site_info.reason, site_info.description,site_statis.siteid, site_statis.visits,site_statis.fans");
		$this->db->from("site");
		$this->db->where("site.id", $siteid);
		$this->db->join("site_info", "site_info.siteid = site.id");
		$this->db->join("site_statis", "site_statis.siteid = site.id");
		$query = $this->db->get();
		$site = $query->row_array();
		if($site) {
			$site["funs"] = $site["fans"];
		}
//		获取被关注数量
//		$site["funs"] = $this->ssm->get_sitefuns($site["id"]);
//		获取访客数量
//		$site["visits"] = $this->ssm->get_sitevisits($site["id"]);
		return $site;
	}
	
	//获取好站基本信息/检测好站是否存在
	function get_site($siteid)
	{
		$query = $this->db->get_where("site", array("id"=>$siteid));
		if($query->num_rows()>0) {
			return $query->row_array();
		}
		return false;
	}
	
	//获取好站详细信息
	function get_siteinfo($siteid)
	{
		$info = array();
		$query = $this->db->get_where("site_info", array("siteid"=>$siteid));
		if($query->num_rows()>0) {
			$info = $query->row_array();
		}
		return $info;
	}
	
	//获取好站基本信息
	function get_domainsite($domain)
	{
		$query = $this->db->get_where("site", array("domain"=>$domain, "complete"=>1));
		if($query->num_rows()>0) {
			$site = $query->row_array();
			$query = $this->db->select("siteid,description,logo,background,color,skin")->get_where("site_info", array("siteid"=>$site["id"]));
			$site["info"] = $query->row_array();
			return $site;
		}
		return false;
	}
	
	//获取用户小站信息
	function get_usersite($userid)
	{
		$query = $this->db->get_where("site", array("userid"=>$userid, "complete"=>1, "status"=>1));
		if($query->num_rows()>0) {
			$site = $query->row_array();
			$query = $this->db->select("*")->get_where("site_info", array("siteid"=>$site["id"]));
			$site["info"] = $query->row_array();
			return $site;
		}
		return false;
	}
	
	//检测是否已经开过好站
	function has_usersite($userid)
	{
		$query = $this->db->get_where("site", array("userid"=>$userid,"complete"=>1));
		if($query->num_rows()>0) {
			$site = $query->row_array();
			$query = $this->db->select("*")->get_where("site_info", array("siteid"=>$site["id"]));
			$site["info"] = $query->row_array();
			return $site;
		}
		return false;
	}
	
	//获取我关注的轻站
	function get_fansite($userid, $limit = 2, $offset = 0)
	{
		$sites = array();
		//$query = $this->db->from("fans")->where("userid", $userid)->order_by("id", "desc")->limit($limit, $offset)->get();
		$this->db->select("fans.*,site.id,site.status");
		$this->db->from("fans");
		$this->db->join("site","site.id=fans.siteid");
		$this->db->where(array("fans.userid"=>$userid, "site.status"=>1));
		$query = $this->db->order_by("fans.id", "desc")->limit($limit, $offset)->get();
		if ($query->num_rows()>0) {
			$result = $query->result_array();
			foreach ($result as $row) {
				$siteid = $row["siteid"];
				$site = $this->get($siteid);
				$site['self'] = $row['self'];
				$sites[] = $site;
			}
		}
		return $sites;
	}
	
	//获取每日一站
	function get_bestrecommend()
	{
		$row = array();
		$query = $this->db->from("site")->where("bestrecommend >", 0)->order_by("bestrecommend", "desc")->limit(1)->get();
		if ($query->num_rows()>0) {
			$row = $query->row_array();
		} else {//随机
			 $query = $this->db->from("site")->where("status", 1)->order_by('id','random')->limit(1)->get();
			 $row = $query->row_array();
		}
		return $row;
	}
	
	//获取推荐
	function get_recommend($limit)
	{
		$result = array();
		$query = $this->db->from("site")->where("recommend >", 0)->order_by("recommend", "desc")->limit($limit)->get();
		if ($query->num_rows()>0) {
			$result = $query->result_array();
		} 
		return $result;
	}
	
	//获取看看
	function get_kankan($limit)
	{
		$result = array();
		$query = $this->db->from("site")->where("kankan >", 0)->order_by("kankan", "desc")->limit($limit)->get();
		if ($query->num_rows()>0) {
			$result = $query->result_array();
		} 
		return $result;
	}
	
	//新开张小站
	function new_site($limit = 100)
	{
		$sites = array();
		$query = $this->db->from("site")->where(array("complete"=>1, "status"=>1))->order_by("regtime","desc")->limit($limit)->get();
		if($query->num_rows()>0) {
			$sites = $query->result_array();
			//前3个获取logo
			foreach ($sites as $k => $site) {
				$siteid = $site['id'];
				$sites[$k]['info'] = $this->get_siteinfo($siteid);
				$sites[$k]['fans'] = $this->ssm->get_sitefuns($siteid);
			}
		}
		return $sites;
	}
	
	//人气轻站
	function hot_site($limit = 100)
	{
		$sites = array();
		$query = $this->db->from("site_statis")->where("status", 1)->order_by("fans","desc")->limit($limit)->get();
		if($query->num_rows()>0) {
			$siteids = $query->result_array();
			//前3个获取logo
			foreach ($siteids as $k => $row) {
				$siteid = $row['siteid'];
				$fans = $row['fans'];
				$sites[$k] = $this->get_site($siteid);
				$sites[$k]['fans'] = $fans;
				$sites[$k]['info'] = $this->get_siteinfo($siteid);
			}
		}
		return $sites;
	}
	
	//上周人气轻站
	function hot_week_site($limit = 100)
	{
		//上周 = 最近一周时间
		$timehack = time() - 24*3600*7;
		$sql = "select DISTINCT siteid from rn_fans where jointime >= $timehack and self =0 limit $limit";
		$query = $this->db->query($sql);
		$results = $query->result_array();
		$siteids = $sites = array();
		foreach ($results as $k => $row) {
			$siteid = $row["siteid"];
			$siteids[] = $siteid;
			//获取新增关注数
			$query = $this->db->get_where("fans", array("siteid"=>$siteid, "jointime >="=>$timehack));
			$newfans = $query->num_rows();
			//获取小站信息
			$sites[$k] = $this->get_site($siteid);
			$sites[$k]['fans'] = $newfans;
			$sites[$k]['info'] = $this->get_siteinfo($siteid);
		}
		//排序
		if (count($sites) > 0) {
			foreach ($sites as $key => $row) {
			    $fans[$key]  = $row['fans'];
			}
			array_multisort($fans, SORT_DESC, $sites);
		}
		return $sites;		
	}
	
	//潜力轻站
	function rand_hot_site($limit = 9)
	{
		$sites = array();
		$hotsites = $this->hot_site();
		count($hotsites) > 9 ? $count = 9 : $count = count($hotsites);
		if($count>0) {
			$rand_key = array_rand($hotsites, $count);
			for ($i=0; $i<$limit; $i++) {
				if(isset($rand_key[$i])) {
					$sites[] = $hotsites[$rand_key[$i]];
				}
			}
		}
		return $sites;
	}
	
	//新张轻站
	function rand_new_site($limit = 9)
	{
		$sites = array();
		$newsites = $this->new_site();
		count($newsites) > 9 ? $count = 9 : $count = count($newsites);
		if($count>0) {
			$rand_key = array_rand($newsites, $count);
			for ($i=0; $i<$limit; $i++) {
				if(isset($rand_key[$i])) {
					$sites[] = $newsites[$rand_key[$i]];
				}
			}
		}
		return $sites;
	}
	
	//看看轻站(随机)
	function kan_site($limit = 5)
	{
		$sites = $hack_siteids = array();
		$sites1 = $this->get_kankan($limit);
		if(count($sites1)>0 && count($sites1) < $limit) {//手动+随机
			foreach ($sites1 as $k => $site) {
				$siteid = $site['id'];
				$hack_siteids[] = $siteid;
				$sites1[$k]['funs'] = $this->ssm->get_sitefuns($siteid);
				$sites1[$k]['info'] = $this->get_siteinfo($siteid);
			}
			//获取随机
			$limit = $limit - count($sites1);
			$query = $this->db->from("site")->where(array("complete"=>1,"status"=>1))->where_not_in("id",$hack_siteids)->order_by('id','random')->limit($limit)->get();
			if ($query->num_rows()>0) {
				$sites2 = $query->result_array();
				foreach ($sites2 as $k => $site) {
					$siteid = $site['id'];
					$sites2[$k]['funs'] = $this->ssm->get_sitefuns($siteid);
					$sites2[$k]['info'] = $this->get_siteinfo($siteid);
				}
				$sites = array_merge($sites1, $sites2);
			}
		} elseif (count($sites1) >= $limit) {//全手动
			foreach ($sites1 as $k => $site) {
				$siteid = $site['id'];
				$hack_siteids[] = $siteid;
				$sites1[$k]['funs'] = $this->ssm->get_sitefuns($siteid);
				$sites1[$k]['info'] = $this->get_siteinfo($siteid);
			}
			$sites = $sites1;
		} elseif (count($sites1) == 0) {//全随机
			$query = $this->db->from("site")->where(array("complete"=>1,"status"=>1))->order_by('id','random')->limit($limit)->get();
			if ($query->num_rows()>0) {
				$sites = $query->result_array();
				foreach ($sites as $k => $site) {
					$siteid = $site['id'];
					$sites[$k]['funs'] = $this->ssm->get_sitefuns($siteid);
					$sites[$k]['info'] = $this->get_siteinfo($siteid);
				}
			}
		}
		
		return $sites;
	}
	
	//获取男女推荐小站
	function position_site($field = "staticgirl", $limit = 9) 
	{
		$sites = array();
		$this->db->from("site");
		if (in_array($field, array("staticboy", "staticgirl"))) {
			$this->db->where($field." >", 0);
			$this->db->order_by($field, "desc");
		} else {
			$this->db->where("staticgirl >",0);
			$this->db->order_by("staticgirl", "desc");
		}
		$this->db->limit($limit);
		$query = $this->db->get();
		if ($query->num_rows()>0) {
			$sites = $query->result_array();
			foreach ($sites as $k => $site) {
				$siteid = $site['id'];
				$sites[$k]['funs'] = $this->ssm->get_sitefuns($siteid);
				$sites[$k]['info'] = $this->get_siteinfo($siteid);
			}
		}
		return $sites;
	}
	
	//推荐轻站(手动+随机)
	function recommend_site($limit = 6)
	{
		$sites = $hack_siteids = array();
		$sites1 = $this->get_recommend($limit);
		if(count($sites1)>0 && count($sites1) < $limit) {//随机
			foreach ($sites1 as $k => $site) {
				$siteid = $site['id'];
				$hack_siteids[] = $siteid;
				$sites1[$k]['funs'] = $this->ssm->get_sitefuns($siteid);
				$sites1[$k]['info'] = $this->get_siteinfo($siteid);
			}
			//获取随机
			$limit = $limit - count($sites1);
			$query = $this->db->from("site")->where(array("complete"=>1,"status"=>1))->where_not_in("id",$hack_siteids)->order_by('id','random')->limit($limit)->get();
			if ($query->num_rows()>0) {
				$sites2 = $query->result_array();
				foreach ($sites2 as $k => $site) {
					$siteid = $site['id'];
					$sites2[$k]['funs'] = $this->ssm->get_sitefuns($siteid);
					$sites2[$k]['info'] = $this->get_siteinfo($siteid);
				}
				$sites = array_merge($sites1, $sites2);
			}
		} elseif (count($sites1) >= $limit) {//全手动
			foreach ($sites1 as $k => $site) {
				$siteid = $site['id'];
				$hack_siteids[] = $siteid;
				$sites1[$k]['funs'] = $this->ssm->get_sitefuns($siteid);
				$sites1[$k]['info'] = $this->get_siteinfo($siteid);
			}
			$sites = $sites1;
		} elseif (count($sites1) == 0) {//全随机
			$query = $this->db->from("site")->where(array("complete"=>1,"status"=>1))->order_by('id','random')->limit($limit)->get();
			if ($query->num_rows()>0) {
				$sites = $query->result_array();
				foreach ($sites as $k => $site) {
					$siteid = $site['id'];
					$sites[$k]['funs'] = $this->ssm->get_sitefuns($siteid);
					$sites[$k]['info'] = $this->get_siteinfo($siteid);
				}
			}
		}
		
		return $sites;	
	}
	
	//关注的人也喜欢(随机)
	function fans_site($limit = 9)
	{
		$sites = array();
		$query = $this->db->from("site")->where(array("complete"=>1,"status"=>1))->order_by('id','random')->limit($limit)->get();
		if ($query->num_rows()>0) {
			$sites = $query->result_array();
			foreach ($sites as $k => $site) {
				$siteid = $site['id'];
				$sites[$k]['funs'] = $this->ssm->get_sitefuns($siteid);
				$sites[$k]['info'] = $this->get_siteinfo($siteid);
			}
		}
		return $sites;	
	}
	
	//计算小站总数
	function count_site()
	{
//		return $this->db->count_all_results("site");
		$query = $this->db->get_where("site", array("complete"=>1,"status"=>1));
		return $query->num_rows();
	}
	
	//检测好站名称是否存在
	function exsits_name($name)
	{
		$query = $this->db->get_where("site", array("name"=>$name,"complete"=>1));
		if($query->num_rows()>0) {
			return true;
		} else {
			return false;
		}
	}
	
	//检测好站域名是否存在
	function exsits_domain($domain)
	{
		$query = $this->db->get_where("site", array("domain"=>$domain));
		if($query->num_rows()>0) {
			return true;
		} else {
			return false;
		}
	}
	
}

/* End of file */